<script setup>
import { ref } from 'vue'
import Swiper from '@/components/Swiper/index.vue'
import { getSmsTemplate, batchSendSms } from '@/api/arrears'
import { ElMessage } from 'element-plus'

const props = defineProps({
  infoList: {
    type: Array,
    default: () => []
  },
})
const emit = defineEmits(['success', 'close'])

const loading = ref(false)
const templateList = ref([])
const getTemplateList = async () => {
  loading.value = true
  try {
    const { data } = await getSmsTemplate()
    templateList.value = data || []
  } catch (error) {
    console.error('获取模板列表失败:', error)
  }
  loading.value = false
}
getTemplateList();

const active = ref(0);
const onChange = (i) => {
  active.value = i;
}

const submitloading = ref(false);
const onSubmit = async () => {
  if (submitloading.value) return;
  submitloading.value = true;
  try {
    await batchSendSms({
      ids: props.infoList.map(v => v.arrears_data_id),
      sms_template_id: templateList.value[active.value].sms_template_id,
    });
    ElMessage.success('发送成功');
    emit('success');
    onClose();
  } catch (error) {
    console.error('提交失败:', error);
  }
  submitloading.value = false;
}
const onClose = () => {
  emit('close');
}
</script>

<template>
  <div>
    <Swiper :categories="templateList" @change="onChange" v-loading="loading" class="!h-[200px]">
      <template #default="{ cat }">
        <div class="p-4 rounded border border-zinc-300 h-full">
          <div class="text-center text-lg mb-4">{{ cat.title }}</div>
          <div class="text-sm text-gray-600 mb-4 whitespace-pre-line">{{ cat.content }}</div>
        </div>
      </template>
    </Swiper>
    <div class="mt-4 text-right">
      <el-button type="primary" @click="onSubmit()" :loading="submitloading">确定</el-button>
      <el-button @click="onClose()">取消</el-button>
    </div>
  </div>
</template>